package com.yitong.equipment.ui.home

import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.fragment.app.*
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.ToastUtils
import com.google.android.material.tabs.TabLayout
import com.gyf.immersionbar.ImmersionBar
import com.yitong.equipment.R
import com.yitong.equipment.base.BaseActivity
import com.yitong.equipment.databinding.ActivityClassifyBinding
import com.yitong.equipment.ui.adapter.PopClassifyAdapter
import com.yitong.equipment.ui.viewmodel.MainViewModel
import com.google.android.material.tabs.TabLayoutMediator

class AllClassifyActivity : BaseActivity<MainViewModel, ActivityClassifyBinding>() {

    private val titles = arrayOf("室内游戏", "户外游戏", "运动活动", "环境创设", "家园共育", "职业素养", "园所管理")
    private var fragments = mutableListOf<Fragment>()
    private lateinit var mediator: TabLayoutMediator

    private var choiceIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_classify)
        initView()
    }

    private fun initView() {
        for (index in titles.indices) {
            fragments.add(ClassifyFragment.getInstance(titles[index]))
        }

        bindingView.mViewPager.run {
            adapter = MyAdapter(this@AllClassifyActivity, fragments)
            offscreenPageLimit = titles.size
            isUserInputEnabled = true
            orientation = ViewPager2.ORIENTATION_HORIZONTAL

            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
                override fun onPageSelected(position: Int) {
                    val tabCount: Int = bindingView.tabLayout.tabCount
                    for (i in 0 until tabCount) {
                        val tab: TabLayout.Tab = bindingView.tabLayout.getTabAt(i)!!
                        val tabView = tab.customView as TextView
                        if (tab.position == position) {
                            tabView.setTextColor(Color.parseColor("#FA6419"))
                            tabView.setBackgroundResource(R.drawable.ashape_ffeee6_16_corner)
                        } else {
                            tabView.setTextColor(Color.parseColor("#242526"))
                            tabView.setBackgroundResource(R.drawable.ashape_f5f7fa_16_corner)
                        }
                    }
                }
            })
        }

        mediator = TabLayoutMediator(bindingView.tabLayout,bindingView.mViewPager) { tab, position ->
            run {
                val view: View = LayoutInflater.from(this@AllClassifyActivity).inflate(R.layout.item_tablayout_header, null)
                var title = view.findViewById<TextView>(R.id.tv_header)
                title.text = titles[position]
                tab.customView = view
                view.setOnClickListener {
                    bindingView.mViewPager.setCurrentItem(position,false)
                    choiceIndex = position
                }
            }
        }
        mediator.attach()


        bindingView.imgMenu.setOnClickListener {
            showPop()
        }

        bindingView.imgBack.setOnClickListener {
            finish()
        }
    }



    fun showPop() {
        val contentView: View = LayoutInflater.from(this).inflate(R.layout.pop_classify, null)
        val recycler: RecyclerView = contentView.findViewById<View>(R.id.recycler) as RecyclerView
        val tv_bot: TextView = contentView.findViewById<View>(R.id.tv_bot) as TextView
        val img_back: ImageView = contentView.findViewById<View>(R.id.img_back) as ImageView
        recycler.layoutManager = GridLayoutManager(this, 4)
        recycler.isNestedScrollingEnabled = false

        val popLMAdapter = PopClassifyAdapter(titles.toMutableList(),choiceIndex)
        recycler.adapter = popLMAdapter

        val popupWindow = PopupWindow(
            contentView,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            true
        )
        popupWindow.isTouchable = true
        popupWindow.setTouchInterceptor { v, event -> false }

        popupWindow.setBackgroundDrawable(BitmapDrawable())
        popupWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
        popupWindow.height = ViewGroup.LayoutParams.MATCH_PARENT

        tv_bot.setOnClickListener { popupWindow.dismiss() }
        img_back.setOnClickListener { popupWindow.dismiss() }

        popupWindow.showAsDropDown(bindingView.rlTitle)

        popLMAdapter.setOnItemClickListener { adapter, view, position ->
            ToastUtils.showShort(position.toString())
            popupWindow.dismiss()
            bindingView.mViewPager.setCurrentItem(position,false)
            choiceIndex = position
        }

    }


    inner class MyAdapter(fragmentActivity: FragmentActivity, private var fragments: List<Fragment>) : FragmentStateAdapter(fragmentActivity) {
        override fun getItemCount(): Int {
            return fragments.size
        }

        override fun createFragment(position: Int): Fragment {
            return fragments[position]
        }

    }

    override fun initImmersionBar() {
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.white).statusBarDarkFont(true).init()
    }

    override fun isImmersionBarEnabled(): Boolean {
        return true
    }
}